<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\warehouse\model;

use think\Model;
use think\Db;

/**
 * 仓库模型
 * @package app\warehouse\model
 */
class Stock extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__GOODS_STOCK__';

    /*
        更新库存

    */
    public function update_stock($skuid,$warehouseid,$num,$update_type){
        if(empty($skuid)) {
            return json(['msg'=>'货品id不能为空','code'=>0]);
        }
        if(empty($warehouseid)) {
            return json(['msg'=>'仓库id不能为空','code'=>0]);
        }
        if(empty($num)) {
            return json(['msg'=>'数量不能为空','code'=>0]);
        }
        if(empty($update_type)) {
            return json(['msg'=>'更新类型不能为空','code'=>0]);
        }
        $data=self::where(['skuid'=>$skuid,'warehouseid'=>$warehouseid])->find();	
        if(empty($data)){
            return json(['msg'=>'尚无入库记录','code'=>0]);
        }
        if($data['waitnum']<$num){
            return json(['msg'=>'可用库存不足','code'=>0]);
        }
        $stockid=$data['id'];
        Db::startTrans(); 
        try{	
            if($update_type=='1'){
                self::where(['stockid'=>$stockid])->setDec('waitnum',$num);
                self::where(['stockid'=>$stockid])->setInc('occupynum',$num);
                self::addstocklog($stockid,"订单提交商品订购量增加".$num."件",$num);
            }elseif($update_type=='2'){
                self::where(['stockid'=>$stockid])->setDec('occupynum',$num);
                self::where(['stockid'=>$stockid])->setInc('waitdeliverynum',$num);
                self::addstocklog($stockid,"订单支付商品待发货量增加".$num."件",$num);
            }elseif($update_type=='3'){
                self::where(['stockid'=>$stockid])->setDec('waitdeliverynum',$num);
                self::where(['stockid'=>$stockid])->setInc('salenum',$num);
                self::addstocklog($stockid,"订单完成商品已售量增加".$num."件",$num);
            }
            Db::commit();			
            return json(['msg'=>'库存更新成功','code'=>1]);
        } catch (\Exception	$e)	{
            Db::rollback(); 
            return json(['msg'=>'库存更新失败','code'=>0]);
        }
        
    }

    public static function holdstock($stockid,$num){
        Db::startTrans(); 
        try{				
            self::where(['stockid'=>$stockid])->setDec('waitnum',$num);
            self::where(['stockid'=>$stockid])->setInc('holdnum',$num);
            self::addstocklog($stockid,"管理员操作商品保留量增加".$num."件",$num);		
            Db::commit();			
            return true;	
        } catch (\Exception	$e)	{
            Db::rollback(); 
            return false;
        }
    }

    public static function frozenstock($stockid,$num){
        Db::startTrans(); 
        try{				
            self::where(['stockid'=>$stockid])->setDec('waitnum',$num);
            self::where(['stockid'=>$stockid])->setInc('frozennum',$num);
            self::addstocklog($stockid,"管理员操作商品冻结量增加".$num."件",$num);		
            Db::commit();			
            return true;	
        } catch (\Exception	$e)	{
            Db::rollback(); 
            return false;
        }
    }

    public static function addstocklog($stockid,$content,$num){
        Db::name("goods_stock_log")->insert([
            'stockid'=>$stockid,
            'content'=>$content,
            'num'=>$num,
            'create_time'=>time()
        ]);
    }

}